home *** CD-ROM | disk | FTP | other *** search
- /* Alain Birtz, 16/2/86 */
- #include "gemdefs.h"
-
- /************************************************************************/
-
- #define NO_WINDOW (-1)
- #define WI_KIND (MOVER|CLOSER|NAME) /* can be moved, closed */
- /* and title exist */
-
- #define Cprnout(a) gemdos(0x5,a)
- #define Dsetdrv(a) gemdos(14,a)
- #define Cprnos() gemdos(0x11)
- #define Fopen(a,b) gemdos(0x3d,a,b)
- #define Fclose(a) gemdos(0x3e,a)
- #define Fread(a,b,c) gemdos(0x3f,a,b,c)
- #define Fwrite(a,b,c) gemdos(0x40,a,b,c)
- #define Fseek(a,b,c) gemdos(0x42,a,b,c)
-
- #define Getrez() (int)xbios(4)
- #define Floprd(a,b,c,d,e,f,g) xbios(8,a,b,c,d,e,f,g)
- #define Flopwr(a,b,c,d,e,f,g) xbios(9,a,b,c,d,e,f,g)
- #define Supexec(a) xbios(38,a)
-
- #define digitoi(n) ( n - ((n<'A') ? '0' : ((n<'a') ? 55 : 87)) )
-
- /************************************************************************/
-
- extern long gemdos();
- extern long xbios();
- extern int gl_apid;
-
- /************************************************************************/
-
- int menu_id ; /* our menu id */
- int handle; /* virtual workstation */
- int phys_handle; /* physical workstation */
- int wi_handle; /* window handle */
- int top_window; /* handle of topped */
- int xdesk,ydesk,hdesk,wdesk;
- int xwork,ywork,hwork,wwork; /* desktop , work areas */
- int msgbuff[8]; /* event message buffer */
- int mx,my; /* mouse x and y position */
- int butdown; /* button state */
- int d; /* dummy variable */
-
- int contrl[12]; /* AES, VDI variable */
- int intin[128];
- int ptsin[128];
- int intout[128];
- int ptsout[128];
- int work_in[11]; /* Input GSX parameter */
- int work_out[57]; /* Output GSX parameter */
-
- int mode; /* read=0, write=1 */
- int drv,side,sec,trk,buf512[512]; /* used in sector editor */
- int open_flag; /* set to 1 if file is open */
- int exit; /* exit flag */
- int clear_flag; /* if 1, erase last message */
- int ed_choice; /* user choice editor */
- int h_chr; /* character height */
- int dev=2; /* device: screen=2, printer=0 */
- int intro_lvl; /* flag in draw(),clicked() */
- int wd[4],ed_scr[4],mess[4]; /* rectangular area */
- int ch[3][4],box[5][4]; /* clickable button */
- char line_str[49]; /* dilplaying line */
- char buf64[64]; /* current byte displaying */
- char *main_ptr,*temp_ptr; /* work pointer */
- long counter; /* first value displaying */
- long file_handle; /* allocated file number */
-
- /************************************************************************/
-
- ltohxs(lg,s,len) /* convert in hexadecimal the value of */
- char s[]; /* lg to a char string s of length */
- long lg; /* 'len' with leading zero */
- int len;
- {
- int i, n;
- long t_lg;
-
- s[len]='\0';
- i=len-1;
-
- {
- while (i>-1)
- {
- t_lg=lg;
- n=(int) (t_lg & 0xFL);
- s[i--]=n+48+(n>9)*7;
- lg >>= 4;
- }
- }
- }
-
- /************************************************************************/
-
- long stol(s,b) /* s is a (string) number in base b */
- char *s; /* return this number (long integer) */
- int b;
- {
- long lg;
- char cc;
-
- lg=0L;
- while((cc = *s++))
- lg=lg*b+digitoi(cc);
- return(lg);
- }
-
- /************************************************************************/
-
- long l_param(x) /* return a long parameter given */
- int x; /* by the user, echo at x char from */
- { /* start of message screen */
- int base;
- char s[11], *t;
-
- graf_mouse(256,0); /* hide mouse */
- readline(s,10,mess[0]+12+x*8,mess[1]+h_chr);
- graf_mouse(257,0); /* show mouse */
-
- if (s[0]=='$')
- {
- base=16;
- t=s+1;
- }
- else
- {
- base=10;
- t=s;
- }
-
- return(stol(t,base));
- }
-
- /************************************************************************/
-
- i_param(x) /* return a integer parameter given */
- int x; /* by the user, echo at x char from */
- { /* start of message screen */
- int n;
-
- n=(int) l_param(x);
- return(n);
- }
-
- /************************************************************************/
-
- file_sel() /* set file_handle > -1 if succesfully */
- { /* openned, -1 if an error occur */
- static char pathname[51]="A:\\*.*";
- char filename[13],*ptr1,*ptr2;
- int but_exit;
-
- fsel_input(pathname,filename,&but_exit);/* file selector */
-
- ptr1=pathname;
- while(*ptr1++) /* find end of string */
- ;
- while(*ptr1!='\\') /* find last \ and */
- *ptr1--; /* replace the */
- ptr2=filename; /* remainder by the */
- while(*++ptr1 = *ptr2++) /* filename to complete */
- ; /* the pathname */
-
- if (but_exit)
- {
- if ((file_handle=Fopen(pathname,2))<0L)
- form_alert(2,"[3][Cannot open this file][Cancel]");
- }
- else
- file_handle = -1L;
- }
-
- /************************************************************************/
-
- time_out()
- {
- long lg;
-
- lg=60000L;
- while (lg--)
- ;
- }
-
- /************************************************************************/
-
- rd_wr() /* read: mode 0, write: mode 1 */
- {
- char *ptr64;
- int i;
-
- if (temp_ptr==buf64)
- return;
-
- ptr64=buf64;
-
- if (mode)
- for (i=0;i<64;i++) /* copy 64 bytes in memory */
- *temp_ptr++ = *ptr64++;
-
- else
- for (i=0;i<64;i++) /* copy 64 bytes to work buffer */
- *ptr64++ = *temp_ptr++;
-
- }
-
- /************************************************************************/
-
- c_peek(mem_ptr) /* read protected memory */
- char *mem_ptr;
- {
- temp_ptr=mem_ptr;
- mode=0;
- Supexec(rd_wr);
- }
-
- /************************************************************************/
-
- c_poke(mem_ptr) /* write protected memory */
- char *mem_ptr;
- {
- temp_ptr=mem_ptr;
- mode=1;
- Supexec(rd_wr);
- }
-
- /************************************************************************/
-
- main()
- {
- appl_init();
- phys_handle=graf_handle(&d,&d,&d,&d);
- menu_id=menu_register(gl_apid," Tiny tool");
- graf_mouse(0,0); /* arrow form */
- wi_handle=NO_WINDOW;
- butdown=1;
-
- multi();
- }
-
- /************************************************************************/
-
- open_vwork() /* open workstation */
- {
- int i, rez;
-
- for(i=0;i<10;work_in[i++]=1);
- work_in[10]=2;
- handle=phys_handle;
- v_opnvwk(work_in,&handle,work_out);
-
-
- rez=Getrez();
- if (!rez)
- {
- form_alert(2,"[3][ |HIGH or MEDIUM resolution only][Cancel]");
- appl_exit();
- exit=1;
- }
-
- h_chr= (rez==2) ? 10:8; /* high:medium resolution */
- vst_height(handle,6,&d,&d,&d,&d);
- }
-
- /************************************************************************/
-
- open_window() /* open window */
- {
- wind_get(0,WF_WORKXYWH,&xdesk,&ydesk,&wdesk,&hdesk);
- wi_handle=wind_create(WI_KIND,xdesk+20,ydesk+30,404,19*h_chr);
- wind_set(wi_handle,WF_NAME," TINY TOOL EDITOR ",0,0);
- wind_open(wi_handle,xdesk+20,ydesk+30,404,19*h_chr);
- }
-
- /************************************************************************/
-
- multi()
- {
- int event, k;
-
- while (1)
- {
- event = evnt_multi(MU_MESAG | MU_BUTTON,
- 1,1,butdown,0,0,0,0,0,0,0,0,0,0,
- msgbuff,0,0,&mx,&my,&d,&d,&d,&d);
-
- wind_update(1);
- wind_get(wi_handle,WF_TOP,&top_window,&d,&d,&d);
-
- if (event & MU_MESAG)
-
- /*..................................................begin switch........*/
- switch (msgbuff[0])
- {
- case WM_NEWTOP:case WM_TOPPED:
- if (msgbuff[3] == wi_handle)
- {
- wind_set(wi_handle,WF_TOP,0,0,0,0);
- draw();
- }
- break;
-
- case AC_CLOSE:
- if ((msgbuff[3] == menu_id)&&(wi_handle != NO_WINDOW))
- {
- v_clsvwk(handle);
- wi_handle = NO_WINDOW;
- }
- break;
-
- case WM_CLOSED:
- if (msgbuff[3] == wi_handle)
- {
- if (open_flag==1) /* close openned file */
- Fclose((int) file_handle);
- wind_close(wi_handle);
- wind_delete(wi_handle);
- v_clsvwk(handle);
- wi_handle = NO_WINDOW;
- }
- break;
-
- case WM_MOVED:
- if (msgbuff[3] == wi_handle)
- {
- wind_set(wi_handle,WF_CURRXYWH,msgbuff[4]
- ,msgbuff[5],msgbuff[6],msgbuff[7]);
- wind_get(wi_handle,WF_WORKXYWH,&xwork,&ywork,&wwork,&hwork);
- draw();
- }
- break;
-
- case AC_OPEN:
- if (msgbuff[4] == menu_id && wi_handle == NO_WINDOW)
- {
- open_vwork();
- if (!exit)
- {
- open_window();
- reset(); /* choice editor screen */
- }
- }
- break;
-
- }
- /*....................................................end switch........*/
-
- if ((event & MU_BUTTON)&&(wi_handle == top_window))
- if (butdown)
- wait(clicked());
- else
- butdown=1;
-
- wind_update(0);
-
- } /* end of while (1) */
-
- }
-
- /************************************************************************/
-
- draw()
- {
- int i;
- static char box_name[5][6]={" \001"," \002","RESET","WRITE","PRINT"};
- static char ch_name[3][7]={"MEMORY","SECTOR"," FILE"};
- static char intr[8][43]={"Parameters in decimal or hexa. (32 or $20)"
- "drive and side are 0 or 1",
- "To modifie some byte, click on their hexa",
- "representation and click WRITE",
- "RESET to renew, PRINT dump to printer",
- "\001 and \002 to see above and below",
- "------------------------------- by A.Birtz",
- "Click on the Editor of your choice"};
-
- wind_get(wi_handle,WF_WORKXYWH,&xwork,&ywork,&wwork,&hwork);
- graf_mouse(256,0); /* hide mouse */
-
- wd[0]=xwork; /* work area */
- wd[1]=ywork;
- wd[2]=xwork+wwork;
- wd[3]=ywork+hwork;
- fill(wd,2,5); /* dark grey diagonal texture */
-
- ed_scr[0]=xwork+10; /* editor area */
- ed_scr[1]=ywork+h_chr;
- ed_scr[2]=xwork+wwork-10;
- ed_scr[3]=ywork+11*h_chr;
-
- if(intro_lvl) /* work screen */
-
- {
- mess[0]=ed_scr[0]; /* message area */
- mess[1]=ywork+hwork-5*h_chr;
- mess[2]=ed_scr[2];
- mess[3]=mess[1]+(3*h_chr)/2;
-
- for(i=0;i<5;i++) /* work button */
- {
- box[i][0]=ed_scr[0]+83*i;
- box[i][1]=ywork+hwork-(5*h_chr)/2;
- box[i][2]=box[i][0]+50;
- box[i][3]=box[i][1]+(3*h_chr)/2;
- fill(box[i],0,0); /* white space */
- v_gtext(handle,box[i][0]+6,box[i][1]+h_chr,box_name[i]);
- }
- }
-
- else /* introduction screen */
-
- {
- fill(ed_scr,0,0); /* white space */
- for(i=0;i<8;i++)
- v_gtext(handle,ed_scr[0]+16,ed_scr[1]+h_chr*(i+2),intr[i]);
-
- for (i=0;i<3;i++)
- { /* choice button */
- ch[i][0]=xwork+(3*i+1)*wwork/10;
- ch[i][1]=ywork+3*hwork/4;
- ch[i][2]=ch[i][0]+2*wwork/10;
- ch[i][3]=ch[i][1]+3*h_chr/2;
- fill(ch[i],0,0); /* white space */
- v_gtext(handle,ch[i][0]+16,ch[i][1]+h_chr,ch_name[i]);
- }
- }
-
- graf_mouse(257,0); /* show mouse */
- }
-
- /************************************************************************/
-
- fill(rect,style,index)
- int rect[], style, index;
- {
- graf_mouse(256,0); /* hide mouse */
- vsf_interior(handle,style); /* fill inside the rect. */
- vsf_style(handle,index); /* with index and style param. */
- v_bar(handle,rect);
- graf_mouse(257,0); /* show mouse */
- }
-
- /************************************************************************/
-
- line(mem_adr,no)
- char *mem_adr;
- long no;
- {
- int i, j, n, h_nib, l_nib;
- char cc,s[9];
-
- ltohxs(no,s,8); /* convert no to hexa. string */
- i=0;
- while(i<2) /* 2 header space */
- line_str[i++]=' ';
- while(i<10) /* no in hexadecimal */
- line_str[i]=s[i++ -2];
- while(i<12) /* 2 more space */
- line_str[i++]=' ';
- for (j=0;j<8;j++) /* 8 byte in hexa and 8 chr */
- {
- cc = *mem_adr++; /* pick the chr in memory */
- n = cc & 0xff; /* low byte */
- h_nib=n>>4; /* first hexa digit of byte */
- line_str[i++]=h_nib+((h_nib>9) ? 55:48);
- l_nib = n & 0xF; /* last hexa digit of byte */
- line_str[i++]=l_nib+((l_nib>9) ? 55:48);
- line_str[i++]=' '; /* space between 2 byte */
- /* displaying character */
- if(n && (dev || (n>31 && n<128)))
- line_str[j+37]=cc;
- else /* if not printable: dot */
- line_str[j+37]='.';
- }
- line_str[i++]=' '; /* one more space before 8 chr */
- for (j=46;j<48;j++) /* last 2 space */
- line_str[j]=' ';
- line_str[j]='\0';
- }
-
- /************************************************************************/
-
- readline(s,max_len,xpos,ypos)
- char *s;
- int max_len,xpos,ypos;
- {
- int i,xy_loc[2];
- char cc;
-
- vsin_mode(handle,4,1);
- xy_loc[0]=xpos;
- xy_loc[1]=ypos;
- i=0;
- do {
- vrq_string(handle,1,1,xy_loc,s+i); /* read char. */
- cc = *(s+i);
-
- if (cc>47 && cc<58 || cc>64 && cc<71 || cc>96 && cc<103 || cc=='$')
- /* deci. or hexa. numb. */
- {
- v_gtext(handle,xpos,ypos,s);
- i++;
- }
-
- if (i && (cc==8 || cc==127)) /* backspace or delete */
- {
- *(s+i)='\0';
- i--;
- *(s+i)=' ';
- v_gtext(handle,xpos,ypos,s); /* erase... */
- *(s+i)='\0';
- }
- } while (cc && i<max_len);
- }
-
- /************************************************************************/
-
- clicked()
- {
- int i,r3;
-
- if (intro_lvl)
- {
- for(i=0;i<5;i++) /* if a clickable box */
- if (inside(box[i]))
- return(1000+i);
-
-
- if (my>ed_scr[1]+h_chr && my<ed_scr[3]-h_chr
- && mx>ed_scr[0]+98 && mx<ed_scr[0]+282)
- { /* if byte in the editor screen */
- r3=(mx-ed_scr[0]-98)/8;
- if (r3%3==2)
- return(-1);
- return(8*((my-ed_scr[1])/h_chr-1) + r3/3);
- }
- }
- else
- for(i=0;i<3;i++) /* if choice editor button */
- if (inside(ch[i]))
- return(2000+i);
-
- return(-1); /* bad click */
- }
-
- /************************************************************************/
-
- inside(rect) /* return 1 if the mouse is */
- int rect[]; /* inside rect, 0 otherwise */
- {
- if (mx>rect[0] && mx<rect[2] && my>rect[1] && my<rect[3])
- return(1);
- else
- return(0);
- }
-
- /************************************************************************/
-
- change(byte_no)
- int byte_no;
- {
- char s[3];
-
- message("Actual value:",0,1);
- ltohxs((long) (buf64[byte_no] & 0xFF),s,2);
- message(s,14,0);
- message("New value:",19,0);
- buf64[byte_no]=i_param(30);
- display(buf64,counter);
- clear_flag=1;
- }
-
- /************************************************************************/
-
- print()
- {
- char *s, *ptr64;
- int i;
- long lg;
-
- if (!Cprnos()) /* check printer status */
- {
- message("Printer not ready, check and retry",0,1);
- return;
- }
-
- ptr64=buf64;
- lg=counter;
- dev=0;
-
- Cprnout('\n'); /* blank line */
- Cprnout('\r');
-
- for(i=0;i<8;i++) /* print no, bytes and char */
- {
- Cprnout('\n'); /* next line */
- Cprnout('\r');
- line(ptr64,lg);
- s=line_str;
- while (*s)
- Cprnout(*s++); /* the line... */
- ptr64 += 8L;
- lg += 8L;
- }
-
- clear_flag=1;
- dev=2;
- }
-
- /************************************************************************/
-
- wait(clk)
- int clk;
- {
- if (clk<0)
- return;
-
- if (intro_lvl) /* work screen */
- {
- if (clear_flag)
- {
- fill(mess,0,0);
- clear_flag=0;
- }
-
- switch(clk) {
- case 1000:up();break; /* up arrow button */
- case 1001:down();break; /* down arrow button */
- case 1002:reset();break; /* RESET button */
- case 1003:write();break; /* WRITE button */
- case 1004:print();break; /* PRINT button */
- default:break; }
-
- if (clk<1000)
- change(clk);
- }
- else
- {
-
- ed_choice=clk-2000; /* choice screen */
- intro_lvl=1;
- draw();
-
- switch(ed_choice) {
- case 0:memory();break; /* MEMORY button */
- case 1:sector();break; /* SECTOR button */
- case 2:file();break; /* FILE button */
- default:break; }
-
- }
- }
-
- /************************************************************************/
-
- write()
- {
- long status;
-
- if (ed_choice==0) /* write byte in memory */
- {
- c_poke(main_ptr);
- status=0L;
- }
-
- if (ed_choice==1) /* write current sector */
- {
- c_poke(main_ptr);
- status=Flopwr(buf512,0L,drv,sec,trk,side,1);
- }
-
- if (ed_choice==2) /* write 64 bytes on file */
- {
- Fseek(main_ptr,(int) file_handle,0);
- status=Fwrite((int) file_handle,64L,buf64);
- }
-
- if (status<0L)
- message("Write error!",0,1);
- else
- message("Done...",0,1);
-
- clear_flag=1;
- }
-
- /************************************************************************/
-
- down()
- {
- main_ptr += 64;
- counter += 64L;
- n_up_down();
- }
-
- /************************************************************************/
-
- up()
- {
- main_ptr -= 64;
- counter -= 64L;
- n_up_down();
- }
-
- /************************************************************************/
-
- n_up_down()
- {
- if (ed_choice==0) /* memory editor */
- display(main_ptr,counter);
-
- if (ed_choice==1) /* sector editor */
- if (counter<0L || counter>512L)
- {
- message("Out of sector",0,1);
- clear_flag=1;
- }
- else
- display(main_ptr,counter);
-
- if (ed_choice==2) /* file editor */
- {
- Fseek(main_ptr,(int) file_handle,0);
- file_read();
- display(buf64,counter);
- }
- }
-
- /************************************************************************/
- reset()
- {
- intro_lvl=0;
- draw();
- }
-
- /************************************************************************/
-
- memory()
- {
- time_out();
- message("Start address:",0,1);
- /* keep starting address */
- main_ptr=l_param(16);
- counter=main_ptr;
- display(main_ptr,counter);
- }
-
- /************************************************************************/
-
- sector()
- {
- message("Drive:",0,1);
- drv=i_param(7);
- message("Side:",11,0);
- side=i_param(17);
- message("Sector:",21,0);
- sec=i_param(29);
- message("Track:",33,0);
- trk=i_param(40);
-
- if (sec_read()<0L)
- return; /* error */
- main_ptr=buf512;
- counter=0L;
- display(main_ptr,counter);
- }
-
- /************************************************************************/
-
- sec_read()
- {
- long status;
-
- status=Floprd(buf512,0L,drv,sec,trk,side,1);
-
- if (status)
- {
- message("Read error!",0,1);
- clear_flag=1;
- return((int) status);
- }
- }
-
- /************************************************************************/
-
- file()
- {
- long status;
-
- if (open_flag==1)
- { /* close openned file */
- Fclose((int) file_handle);
- open_flag=0;
- }
-
- file_sel(); /* choice a file */
-
- draw();
- if (file_handle<0L)
- return; /* error */
-
- time_out();
- message("File offset (beginning=0)",0,1);
- /* keep file offset */
- main_ptr=l_param(30);
-
- open_flag=1;
- counter=main_ptr;
- Fseek(counter,(int) file_handle,0);
- file_read();
- display(buf64,counter);
- }
-
- /************************************************************************/
-
- file_read()
- {
- int i;
- long status;
-
- status=Fread((int) file_handle,64L,buf64);
- if (status<0L) /* status < 0L mean error */
- {
- message("Read error!",0,1);
- clear_flag=1;
- return;
- }
- if (status<64L) /* status > -1L mean # of read */
- for (i=(int) status;i<64;i++)
- buf64[i]='\0'; /* fill tail with zero */
- }
-
- /************************************************************************/
-
- display(ptr_mem,lg)
- long lg;
- char *ptr_mem;
- {
- char *ptr64;
- int i;
-
- c_peek(ptr_mem); /* read in supervisor mode */
-
- ptr64=buf64;
-
- graf_mouse(256,0); /* hide mouse */
- fill(ed_scr,0,0); /* blank space */
-
- for(i=2;i<10;i++) /* print no, bytes and char */
- {
- line(ptr64,lg);
- v_gtext(handle,ed_scr[0]+2,ed_scr[1]+h_chr*i,line_str);
- ptr64 += 8L;
- lg += 8L;
- }
-
- graf_mouse(257,0); /* show mouse */
- }
-
- /************************************************************************/
-
- message(text,chr_1_pos,clear)
- char *text;
- int chr_1_pos, clear;
- {
- if (clear)
- fill(mess,0,0); /* white space */
- graf_mouse(256,0); /* hide mouse */
- v_gtext(handle,mess[0]+12+8*chr_1_pos,mess[1]+h_chr,text);
- /* print message */
- graf_mouse(257,0); /* show mouse */
- }
-
-